package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdTeamExpertgroupDao;
import com.ccp.dev.qbdms.model.WQbdTeamExpertgroup;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 专家 信息操作服务类
 *
 * @author zhl
 * @date 2020-07-03 10:56:43
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdTeamExpertgroupService extends BaseService<WQbdTeamExpertgroup> {

    @Resource
    private WQbdTeamExpertgroupDao dao;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
    List<WQbdTeamExpertgroup> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WQbdTeamExpertgroup data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    /**
     * 查询上报流程的业务数据
     *
     * @param dataId
     * @param tableName
     * @return
     */
    public List<WQbdTeamExpertgroup> reportList(String dataId, String tableName) {
        return dao.reportList(dataId, tableName);
    }



    public List<WQbdTeamExpertgroup> queryAll(QueryFilter queryFilter) {
        return dao.queryAll(queryFilter);
    }

    public void addImportData(List<WQbdTeamExpertgroup> list) {
        ZhiLiangConst.addCommonFields(list);
        //上报状态
        BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
        list.forEach(a -> {
            String mainId = ProjectUUIDUtils.getUUIDFor32();
            a.setId(mainId);
            //上报状态
            if (BaseUtil.SystemLevel.GROUPLEVEL.equals(currentSystemLevel)) {
                a.setReportStatus(ZhiLiangConst.GROUP_ADD);
            } else if (BaseUtil.SystemLevel.HOSPITALLEVEL.equals(currentSystemLevel)) {
                a.setReportStatus(ZhiLiangConst.REPORT_STATUS_GROUPTOBEREPORTED);
            } else if (BaseUtil.SystemLevel.PLANTLEVEL.equals(currentSystemLevel)) {
                a.setReportStatus(ZhiLiangConst.getDataInitStatusToHospital());
            }

            try{
                a.setAge((long) getAge(a.getIdnumber()));
            }catch (Exception e){
                e.printStackTrace();
            }

        });
        if (!list.isEmpty()) {
            dao.addList(list);
        }
    }


    public int getAge(String idCard) {
        int year = 0, month = 0, day = 0;

        // 截取身份证号获得生日信息
        if(idCard.length() == 18){
            year = Integer.valueOf(idCard.substring(6, 10));
            month = Integer.valueOf(idCard.substring(10, 12));
            day = Integer.valueOf(idCard.substring(12,14));
        } else if(idCard.length() == 15){
            year = Integer.valueOf("19" + idCard.substring(6, 8));
            month = Integer.valueOf(idCard.substring(8, 10));
            day = Integer.valueOf(idCard.substring(10, 12));
        }

        // 当前时间
        Integer currentYear = 0, currentMonth = 0, currentDay = 0;
        Calendar date = Calendar.getInstance();
        currentYear =date.get(Calendar.YEAR);
        currentMonth = date.get(Calendar.MONTH) + 1;
        currentDay = date.get(Calendar.DAY_OF_MONTH);

        int age = 0;
        if(year != 0 && month != 0 && day != 0){
            // 未过生日
            if(month > currentMonth || (month == currentMonth && day > currentDay)){
                age = currentYear - year - 1;
            } else {    // 已过生日
                age = currentYear - year;
            }
        }
        return age;
    }

    /**
     * 根据主表id查询主表加子表数据
     *
     * @param id 主表id
     * @return
     */
    public WQbdTeamExpertgroup getParentAndChilden(String id) {
        return getOneById(id);
    }

    /**
     * 保存接口传输的数据
     *
     * @param wQbdTeamExpertgroup 主表model,有子表的包含子表数据
     * @return
     */
    public int saveAll(List<WQbdTeamExpertgroup> wQbdTeamExpertgroup,List<ExtendSysFile> extendSysFileList){
        ReadAttachmentUtil.getEntityData(wQbdTeamExpertgroup,extendSysFileList);
        wQbdTeamExpertgroup.forEach(model -> {
            model.setBelongdeptname(StringUtil.isNotEmpty(model.getFilldeptHospitalId()) ? BaseUtil.getCurrentTenOrgCodePath(model.getFilldeptHospitalId()) : BaseUtil.getCurrentTenOrgCodePath(model.getFilldeptid()));
            if (!BeanUtils.isEmpty(dao.getById(model.getId()))) {
                // 删除重复的
                dao.delById(model.getId());
            }
        });
        return addList(wQbdTeamExpertgroup);
    }




}
